package com.midian.common.job;
import org.apache.log4j.Logger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.springframework.scheduling.quartz.JobDetailAwareTrigger;

/**
 * 
 * @Title:工具类  
 * @Desription:定时任务工具类,负责管理系统中定时任务的启动和停止
 * @Company:MDKG
 * @ClassName:QuarzJobHelper.java
 * @Author:allan
 * @CreateDate:2013-6-7 下午6:13:30  
 * @UpdateUser:allan 
 * @Version:0.1
 */
public class QuarzJobHelper {
	/**任务调度对象*/
	private Scheduler scheduler;
	/**Log4j日志对象*/
	private static final Logger log = Logger.getLogger(QuarzJobHelper.class);
	public Scheduler getScheduler() {
		return scheduler;
	}

	public void setScheduler(Scheduler scheduler) {
		this.scheduler = scheduler;
	}

	/**
	 * 
	 * @ClassName: QuarzJobHelper.java
	 * @Description: 添加新任务并进行执行
	 * @Author:allan
	 * @CreateDate:2013-6-7 下午6:14:16
	 * @param detail 任务具体对象
	 * @param trigger 触发的规则
	 * @throws SchedulerException
	 */
	public void addJob(JobDetail detail,Trigger trigger) throws SchedulerException{
		scheduler.scheduleJob(detail, trigger);
		if(!scheduler.isStarted()){
			if(log.isDebugEnabled()){
				log.debug("任务管理器未启动");
			}
			scheduler.start();
		}else{
			if(log.isDebugEnabled()){
				log.debug("任务管理器启动正常");
			}
		}
	}
	
	/**
	 * 
	 * @ClassName: QuarzJobHelper.java
	 * @Description: 添加新任务并进行执行
	 * @Author:allan
	 * @CreateDate:2013-6-7 下午6:14:42
	 * @param trigger spring中的trigger对象
	 * @throws SchedulerException
	 */
	public void addJob(Trigger trigger) throws SchedulerException{
		JobDetail jobDetail = null;
		if(trigger instanceof JobDetailAwareTrigger){
			jobDetail=((JobDetailAwareTrigger)trigger).getJobDetail();
		}
		addJob(jobDetail,trigger);
	}
	
	/**
	 * 
	 * @ClassName: QuarzJobHelper.java
	 * @Description: 移除指定的任务
	 * @Author:allan
	 * @CreateDate:2013-6-7 下午6:16:02
	 * @param jobName 任务名称
	 * @param jobGroupName 任务组名称
	 * @throws SchedulerException
	 */
	public void removeJob(String jobName,String jobGroupName) throws SchedulerException{
		scheduler.deleteJob(jobName, jobGroupName);
	}	
	
	/**
	 * 
	 * @ClassName: QuarzJobHelper.java
	 * @Description: 移除指定的任务
	 * @Author:allan
	 * @CreateDate:2013-6-7 下午6:16:21
	 * @param detail 任务信息
	 * @throws SchedulerException
	 */
	public void removeJob(JobDetail detail) throws SchedulerException{
		removeJob(detail.getName(),detail.getGroup());
		log.info("任务管理器移除任务"+detail.getName());
	}
	
	/**
	 * 
	 * @ClassName: QuarzJobHelper.java
	 * @Description: 重置指定的任务触发器
	 * @Author:allan
	 * @CreateDate:2013-6-7 下午6:16:53
	 * @param trigger 规则
	 * @throws SchedulerException
	 */
	public void changeTrigger(Trigger trigger)throws SchedulerException{
		JobDetail jobDetail = new JobDetail();
		//删除原有的触发任务，添加新的触发任务
		if(trigger instanceof JobDetailAwareTrigger){
			jobDetail=((JobDetailAwareTrigger)trigger).getJobDetail();
		}
		removeJob(jobDetail);
		addJob(jobDetail, trigger);
		log.info("任务管理器修改触发器:"+trigger.getName());
	}
	
	/**
	 * 
	 * @ClassName: QuarzJobHelper.java
	 * @Description: 删除指定的触发器
	 * @Author:allan
	 * @CreateDate:2013-6-7 下午6:17:25
	 * @param trigger 规则
	 * @throws SchedulerException
	 */
	public void removeTrigger(Trigger trigger)throws SchedulerException{
		scheduler.unscheduleJob(trigger.getName(), trigger.getGroup());
		log.info("任务管理器移除触发器:"+trigger.getName());
	}
	
	/**
	 * 
	 * @ClassName: QuarzJobHelper.java
	 * @Description: 手动销毁全部任务
	 * @Author:allan
	 * @CreateDate:2013-6-7 下午6:17:38
	 * @throws SchedulerException
	 */
	private void destory()throws SchedulerException{
		scheduler.shutdown();
	}
}
